<template>
  <el-container class="container">
    <el-header>
      <div>
        <img src="../assets/logo.png" alt="" class="logo" />
        <span class="xitong">电商管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '83px' : '200px'">
        <div class="collapseButton" @click="collapse">
          <span>{{ isCollapseText }}</span>
        </div>


        <!-- class="el-menu-vertical-demo"
          @open="handleOpen"
          @close="handleClose"
          default-active="$route.path"
          active-text-color="#3398FF" 点击子菜单所变成的颜色
          unique-opened 只打开1个子菜单,加:是表示true是一个值不然会变成字符串，默认是false 只加上unique-opened也代表true
         -->
        <el-menu default-active="$route.path" background-color="#333744" text-color="#fff" active-text-color="#3398FF"
          :unique-opened="true" :collapse="isCollapse" :collapse-transition="false" router @close="handleClose">
          <!-- 动态的属性前面加: 比如 index、class -->
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <!-- 菜单选项图标 -->
              <i :class="iconsObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 1.index表示跳转的路径 '/' 可加可不加
                 2. 现在必须加：如果不加，点击菜单页面(商品列表页面添加按钮)按钮跳转到菜单以外页面 再重新点击菜单选项 就可能会发生路径叠加
                    https://blog.csdn.net/m0_55728090/article/details/124104951-->
            <el-menu-item :index=" '/'  + subItem.path" v-for="subItem in item.children" :key="subItem.id"
              @click="saveActivePath('/' + subItem.path)">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menuList: [],
      iconsObj: {
        125: "iconfont icon-user",
        103: "iconfont icon-lock_fill",
        101: "iconfont icon-shangpin",
        102: "iconfont icon-danju",
        145: "iconfont icon-baobiao",
      },
      treeData: {
        "id": 160,
        "authName": "树形结构数据",
        "path": "treeData",
        "children": [],
        "order": null
      },

      rightTree: {
        "id": 161,
        "authName": "权限树形结构数据",
        "path": "rightTree",
        "children": [],
        "order": null
      },

      params2: {
        "id": 162,
        "authName": "分类参数2",
        "path": "paramsV2",
        "children": [],
        "order": null
      },





      isCollapse: false,
      isCollapseText: '| | |',
      activePath: ''

    };
  },
  created() {
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    handleClose(key, keyPath) {
      console.log(key, keyPath);
    },
    saveActivePath(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      console.log( 'activePath is ' + activePath)
      this.activePath = activePath
    },
    collapse() {
      this.isCollapse = !this.isCollapse
      //  if(this.isCollapseText === '| | |') return this.isCollapseText = '|||'
      //  this.isCollapseText = '| | |'
      //  第2种
      if (!this.isCollapse) return this.isCollapseText = '| | |';
      this.isCollapseText = '|||'

    },
    logout() {
      window.sessionStorage.clear()
      //window.sessionStorage.removeItem("token");
      //this.$router.push('/login')
      this.$router.replace("/login");
    },
    async getMenuList() {
      const { data: resData } = await this.$http.get("menus");
      //console.log(resData);
      console.log(this.menuList);
      console.log("准备赋值");
      if (resData.meta.status !== 200)
        return this.$message.error(resData.meta.msg);
      this.menuList = resData.data;
      //增加一个数据写死的页面(数据库不知道怎么配置)
      this.menuList.forEach(ele => {
        if (ele.id === 103) {
          ele.children.push(this.rightTree)
          ele.children.push(this.treeData)
        }
        // 第2个分类参数页面
        if (ele.id === 101) {
          ele.children.push(this.params2)
        }


      })
      console.log(this.menuList);
    },
  },
};
</script>

<style lang="less" scoped>
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0px;
  padding-right: 0px;

  color: white;
  font-size: 20px;

  // 子标签选择器
  >div {
    display: flex;
    align-items: center;
  }
}

.logo {
  padding-right: 15px;
  height: 60px;
  width: 60px;
}

.el-aside {
  background-color: #333744;

  // 子类选择器
  .el-menu {
    border-right: solid 0px
  }

}

.el-main {
  background-color: #eaedf1;
}

.container {
  height: 100%;
}

.collapseButton {
  background-color: #4a5064;
  text-align: center;
  color: white;
  height: 20px;
  // >span{
  //   padding-left: 5px;
  // }
}


.iconfont {
  margin-right: 10px;
}
</style>
